package com.cskaoyan.controller.admin;

import com.cskaoyan.bean.BasePageInfo;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.bean.MarketCoupon;
import com.cskaoyan.bean.MarketCouponUser;
import com.cskaoyan.bean.param.CommonData;
import com.cskaoyan.service.admin.MarketCouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Date;
import java.util.UUID;

/**
 * @author RXC
 * @date 2022/6/27 19:06
 */
@RestController
@RequestMapping("admin/coupon")
public class AdminCouponController {

    @Autowired
    MarketCouponService marketCouponService;

    /**
     * 显示优惠券内容，兼顾查询
     * @param name,type,status,basePageInfo
     * @return CommonData
     */
    @RequestMapping("list")
    public BaseRespVo list(String name, Short type, Short status, BasePageInfo basePageInfo) {
        CommonData<MarketCoupon> data = marketCouponService.list(name, type, status, basePageInfo);
        return BaseRespVo.ok(data);
    }

    /**
     * 添加优惠券
     * po接收即可,依旧是多了id, addTime, updateTime三项
     * id需要接收，后两项手动初始值相同
     * todo 注意起止时间以及满减金额的校验
     * @param marketCoupon
     * @return
     */
    @RequestMapping("create")
    public BaseRespVo create(@RequestBody MarketCoupon marketCoupon) {
        // 起止时间判断
        BaseRespVo dateBaseRespVo = getDateBaseRespVo(marketCoupon);
        if (dateBaseRespVo != null) return dateBaseRespVo;

        // 数值判断
        BaseRespVo numBaseRespVo = getNumBaseRespVo(marketCoupon);
        if (numBaseRespVo != null) return numBaseRespVo;

        // 添加兑换码
        UUID uuid = UUID.randomUUID();
        String uuidStr = uuid.toString();
        String substring = uuidStr.substring(0, 8);
        String code = substring.toUpperCase();
        marketCoupon.setCode(code);

        marketCoupon = marketCouponService.create(marketCoupon);
        return BaseRespVo.ok(marketCoupon);
    }

    /**
     * 显示优惠券详情
     * @param id
     */
    @RequestMapping("read")
    public BaseRespVo read(Integer id) {
        MarketCoupon marketCoupon = marketCouponService.read(id);
        if (marketCoupon == null) {
            return BaseRespVo.invalidData("该优惠券已不存在");
        }
        return BaseRespVo.ok(marketCoupon);
    }

    /**
     * todo 没有数据所以没有测试
     * 优惠券详情页显示用户
     * @param couponId,userId,status,basePageInfo
     */
    @RequestMapping("listuser")
    public BaseRespVo listUser(Integer couponId, Integer userId, Short status, BasePageInfo basePageInfo) {
        // todo 注意状态码的转变
        /*if (status == 0) {
            status = 0;
        } else if (status == 1) {
            status = 2;
        }*/
        CommonData<MarketCouponUser> data = marketCouponService.listUser(couponId, userId, status, basePageInfo);
        return BaseRespVo.ok(data);
    }

    /**
     * 更新优惠券信息，只需要修改updateTime
     * @param marketCoupon
     * @return marketCoupon
     */
    @RequestMapping("update")
    public BaseRespVo update(@RequestBody MarketCoupon marketCoupon) {
        // 起止时间判断
        BaseRespVo dateBaseRespVo = getDateBaseRespVo(marketCoupon);
        if (dateBaseRespVo != null) return dateBaseRespVo;

        // 满减金额
        BaseRespVo discountRespVo = getNumBaseRespVo(marketCoupon);
        if (discountRespVo != null) return discountRespVo;

        marketCoupon = marketCouponService.update(marketCoupon);
        return BaseRespVo.ok(marketCoupon);
    }

    /**
     * 删除优惠券
     * @param marketCoupon
     */
    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody MarketCoupon marketCoupon) {
        int delete = marketCouponService.delete(marketCoupon);
        return BaseRespVo.ok();
    }

    // 起止时间判断
    private BaseRespVo getDateBaseRespVo(MarketCoupon marketCoupon) {
        // 起止时间
        Date startTime = marketCoupon.getStartTime();
        Date endTime = marketCoupon.getEndTime();
        Date currentDate = new Date();
        if (marketCoupon.getTimeType() == 0) {
            marketCoupon.setStatus(new Short("0"));
        }
        if (marketCoupon.getTimeType() == 1) {
            if (endTime != null && startTime != null && endTime.compareTo(startTime) < 0) {
                return BaseRespVo.invalidParameter("结束时间早于开始时间");
            }
            if (endTime != null && endTime.compareTo(currentDate) < 0) {
                marketCoupon.setStatus(new Short("1"));
            } else if (endTime != null && endTime.compareTo(currentDate) > 0) {
                marketCoupon.setStatus(new Short("0"));
            }
        }
        return null;
    }

    private BaseRespVo getNumBaseRespVo(MarketCoupon marketCoupon) {
        // 数值基础判断
        Short days = marketCoupon.getDays();
        // 这两者的值可以为0
        Short limit = marketCoupon.getLimit();
        Integer total = marketCoupon.getTotal();
        BigDecimal min = marketCoupon.getMin();
        BigDecimal discount = marketCoupon.getDiscount();

        if (marketCoupon.getTimeType() == 0 && days == null || limit == null || total == null || min == null || discount == null) {
            return BaseRespVo.invalidParameter("数值不能为空");
        }

        if (limit < 0 || total < 0) {
            return BaseRespVo.invalidParameter("每人限领或优惠券数量不能为负数");
        }

        if (min.compareTo(new BigDecimal(0)) <= 0 || discount.compareTo(new BigDecimal(0)) <= 0) {
            return BaseRespVo.invalidParameter("最低消费和满减金额必须为非负数");
        }

        if (marketCoupon.getTimeType() == 0 && days <= 0) {
            return BaseRespVo.invalidParameter("天数必须为非负数");
        }

        // 满减金额
        if (min.compareTo(discount) <= 0) {
            return BaseRespVo.invalidParameter("满减金额必须低于最低消费");
        }
        return null;
    }
}
